package com.lfg.leetcode.leetcode100;

public class No122maxProfit {

    public static void main(String[] args) {
        int[] data = new int[]{7,1,5,3,6,4};
        int ret = maxProfit(data);
        int ret2 = maxProfit2(data);
        System.out.println("[" + ret + "]");
        System.out.println("[" + ret2+ "]");
    }


    public static int maxProfit(int[] prices) {

       int[][] dp = new int[prices.length][2];
       dp[0][0] = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            // 当前持有的最大收益，之前售出或者当前售出的收益
            dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]);

            // 当前空仓的收益
            dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]);

        }
        return dp[prices.length-1][0];
    }

    public static int maxProfit2(int[] prices) {
        int total = 0;
        for (int i = 1; i < prices.length; i++) {
            int diff = prices[i] - prices[i-1];
            if(diff>0){
                total+= diff;
            }

        }
        return total;
    }
}
